﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using ComboBox = System.Windows.Controls.ComboBox;

namespace 集合控件
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<MenuModel> Menus { get; set; } = new List<MenuModel>();

        public MainWindow()
        {
            InitializeComponent();

            List<Person> list = new List<Person>();

            list.Add(new Person { Name = "张三", Age = 22, Address = "广东省廉江市车板镇大坝村" });
            list.Add(new Person { Name = "李四", Age = 23, Address = "江西省景德镇市市辖区" });
            list.Add(new Person { Name = "王五", Age = 24, Address = "上海市虹口区" });

            combobox2.ItemsSource = list;


            for (int i = 0; i < 5; i++)
            {
                MenuModel parent = new MenuModel();
                parent.Name = $"一级菜单 ";
                for (int j = 0; j < 5; j++)
                {
                    MenuModel child = new MenuModel();
                    child.Name = $"二级菜单 ";
                    parent.Children.Add(child);
                }
                Menus.Add(parent);
            }

            _Menu2.ItemsSource = Menus;
        }

        private void combobox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            _TextBlockTel.Text = combobox1.Text;
        }

        private void combobox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox combobox = sender as ComboBox;
            if (combobox == null) return;

            var person = combobox.SelectedItem as Person;
            if (person == null) return;

            _TextBlockName.Text = person.Name;
            _TextBlockAge.Text = person.Age + "岁";
            _TextBlockAddress.Text = person.Address;
        }


        private void _tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var tab = sender as System.Windows.Controls.TabControl;
            var item = tab.SelectedItem as TabItem;
            var content = tab.SelectedContent;
            _textBlock.Text = "标题:" + item.Header.ToString() + " 内容:" + content;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
            {
                _TextBox.Text = dialog.SelectedPath;
                LoadTreeView(dialog.SelectedPath);

            }
        }

        private void LoadTreeView(string rootPath)
        {
            // 设置根节点
            TreeViewItem rootNode = new TreeViewItem();
            rootNode.Header = "根目录";

            // 加载子文件夹和文件
            LoadSubDirectory(rootNode, rootPath);

            // 将根节点添加到TreeView中
            _TreeView.Items.Add(rootNode);
        }

        private void LoadSubDirectory(TreeViewItem node, string path)
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(path);

                // 加载子文件夹
                foreach (DirectoryInfo subDirInfo in dirInfo.GetDirectories())
                {
                    TreeViewItem subNode = new TreeViewItem();
                    subNode.Header = subDirInfo.Name;

                    LoadSubDirectory(subNode, subDirInfo.FullName);

                    node.Items.Add(subNode);
                }

                // 加载文件
                foreach (FileInfo fileInfo in dirInfo.GetFiles())
                {
                    TreeViewItem subNode = new TreeViewItem();
                    subNode.Header = fileInfo.Name;

                    node.Items.Add(subNode);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void _TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            // 获取选中的节点
            TreeViewItem selectedNode = _TreeView.SelectedItem as TreeViewItem;

            // 显示选中节点的Header
            if (selectedNode != null)
            {
                System.Windows.MessageBox.Show(selectedNode.Header.ToString());
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            _TextBlock.Text = $"你单击了 {item.Header}";
        }
    }


    /// <summary>
    /// 主菜单的实体
    /// </summary>
    public class MenuModel
    {
        public string Name { get; set; }
        public List<MenuModel> Children { get; set; } = new List<MenuModel>();
        public string View { get; set; }
    }
}